//
//  YCHomeViewController.swift
//  WeiboDemo
//
//  Created by 尹成 on 2017/1/6.
//  Copyright © 2017年 yc. All rights reserved.
//

import UIKit


/// 定义全局常亮,尽量使用private修饰,否则随处可以访问
private let cellId = "HomeCellId"

class YCHomeViewController: YCMainViewController {
    
    fileprivate lazy var listViewModel = YCStatusListViewModel()
    
    override func loadData() {
        
        listViewModel.loadStatus(isPullup: isPullup) { (isSuccess,shouldRefresh) in
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            if shouldRefresh {
                self.tableView?.reloadData()
            }
        }
        
    }
}

// MARK: - 表格数据源方法
extension YCHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId)
        //2.设置cell
        cell?.textLabel?.text = listViewModel.statusList[indexPath.row].text
        //3.返回cell
        return cell!
    }
}

extension YCHomeViewController{
    override func setTableView() {
        super.setTableView()
        yctitleItem.leftBarButtonItem = UIBarButtonItem(normalImage: "navigationbar_friendattention",
                                                        highlightedImage: "navigationbar_friendattention_highlighted",
                                                        target: self,
                                                        action: #selector(showFirendsAction))
        
        //注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        setupNavTitle()
    }
    
    /// 设置导航栏标题
    private func setupNavTitle(){
        let titleString = YCNetworkManager.shared.userAccount.screen_name
        let button = YCTitleButton(title: titleString)
        yctitleItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitle), for: .touchUpInside)
    }
    
    @objc private func showFirendsAction() {
        let vc = YCFriendsViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc private func clickTitle(bt:YCTitleButton){
        bt.isSelected = !bt.isSelected
    }
    
}
